package com.cj.yudada.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cj.yudada.model.dto.question.QuestionContentDto;
import com.cj.yudada.model.entity.App;
import com.cj.yudada.model.entity.Question;
import com.cj.yudada.model.entity.ScoringResult;
import com.cj.yudada.model.entity.UserAnswer;
import com.cj.yudada.model.vo.QuestionVO;
import com.cj.yudada.service.AppService;
import com.cj.yudada.service.QuestionService;
import com.cj.yudada.service.ScoringResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 自定义评分   的 打分
 * @author cc
 * @date 2024-11-10 18:15
 **/
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)
public class CustomScoreScoringStrategyImpl implements ScoringStrategy {

    @Autowired
    AppService appService;

    @Autowired
    QuestionService questionService;

    @Autowired
    ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) {

        Long appId = app.getId();

        Question question = questionService.getOne(Wrappers.lambdaQuery(Question.class).eq(Question::getAppId, appId));

        List<ScoringResult> scoringResultList = scoringResultService.list(Wrappers.lambdaQuery(ScoringResult.class).eq(ScoringResult::getAppId, appId).orderByDesc(ScoringResult::getResultScoreRange));


        // 统计用户总得分
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDto> questionContent = questionVO.getQuestionContent();
        int totalScore = 0;
        for (QuestionContentDto questionContentDto : questionContent) {

            for (String choice : choices) {

                for (QuestionContentDto.Option option : questionContentDto.getOptions()) {
                    if (choice.equals(option.getKey())) {
//                        int score = option.getScore();
                        // 分数可能没填写
                        Integer score = Optional.of(option.getScore()).orElse(0);
                        totalScore += score;
                    }
                }

            }
        }


        ScoringResult maxScoringResult = scoringResultList.get(0);
        // 获取命中的结果
        for (ScoringResult scoringResult : scoringResultList) {
            if (totalScore  >= scoringResult.getResultScoreRange()){

                maxScoringResult = scoringResult;
                break;
            }
        }


        // 构造返回值
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());

        userAnswer.setResultScore(totalScore);

        return userAnswer;

    }
}
